Restore netplan config even when exception is raised (bugfix)#1777
Restore netplan config even when exception is raised (bugfix)#1777
Conversation
… is raised Modify function `netplan_apply_config` to raise SystemExit if the subprocess call to netplan failed for any reason. By doing this, the `main` function can then use the `try...except...finally` clause, which ensures: - journal entries are still printed out to the log if there is any SystemExit - test config removal and original config restoration are ran regardless of what happens A `try...finally` clause is also used when applying the netplan config after restoring the original files to make sure journal entries are output to log regardless of the outcome.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1777 +/- ##
==========================================
+ Coverage 49.63% 49.98% +0.34%
==========================================
Files 377 378 +1
Lines 40630 40786 +156
Branches 6830 6860 +30
==========================================
+ Hits 20168 20388 +220
+ Misses 19740 19672 -68
- Partials 722 726 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Tested on 202412-36142, the config of netplan is deleted if there is no checkbox.conf. |
|
After adding the fix in commit 9711a32, the execution seems to run correctly, that is, if SSID is omitted, the test will fail with an error message ("A SSID is required for the test"), but the config will be restored. |
Hook25
left a comment
There was a problem hiding this comment.
This doesn't look very good to me ngl. Catching BaseExceptions like SystemExit is in general not great. My best solution if I had to choose is to make the config a context manager, that would be super gorgeous
with generate_test_config(...) as config_data:
...If you really don't want to do that, you can always use atexit, but I would advise you against that, it is not super clean as it introduces a global side effect to closing the application which is always a bit yucky if you can avoid it
|
I tested my latest changes (90eebef) on a device, and the tests fail without an SSID, but the config files are restored: |
* Delete test config and restore original config even when an exception is raised Modify function `netplan_apply_config` to raise SystemExit if the subprocess call to netplan failed for any reason. By doing this, the `main` function can then use the `try...except...finally` clause, which ensures: - journal entries are still printed out to the log if there is any SystemExit - test config removal and original config restoration are ran regardless of what happens A `try...finally` clause is also used when applying the netplan config after restoring the original files to make sure journal entries are output to log regardless of the outcome. * netplan apply function does not return boolean anymore Fix #1674 * Catch FileNotFoundError when netplan test config absent * Use context managers to handle config backup/restoration * Make sure test config is deleted * Update unit tests
* Delete test config and restore original config even when an exception is raised Modify function `netplan_apply_config` to raise SystemExit if the subprocess call to netplan failed for any reason. By doing this, the `main` function can then use the `try...except...finally` clause, which ensures: - journal entries are still printed out to the log if there is any SystemExit - test config removal and original config restoration are ran regardless of what happens A `try...finally` clause is also used when applying the netplan config after restoring the original files to make sure journal entries are output to log regardless of the outcome. * netplan apply function does not return boolean anymore Fix #1674 * Catch FileNotFoundError when netplan test config absent * Use context managers to handle config backup/restoration * Make sure test config is deleted * Update unit tests
* Delete test config and restore original config even when an exception is raised Modify function `netplan_apply_config` to raise SystemExit if the subprocess call to netplan failed for any reason. By doing this, the `main` function can then use the `try...except...finally` clause, which ensures: - journal entries are still printed out to the log if there is any SystemExit - test config removal and original config restoration are ran regardless of what happens A `try...finally` clause is also used when applying the netplan config after restoring the original files to make sure journal entries are output to log regardless of the outcome. * netplan apply function does not return boolean anymore Fix #1674 * Catch FileNotFoundError when netplan test config absent * Use context managers to handle config backup/restoration * Make sure test config is deleted * Update unit tests
* Delete test config and restore original config even when an exception is raised Modify function `netplan_apply_config` to raise SystemExit if the subprocess call to netplan failed for any reason. By doing this, the `main` function can then use the `try...except...finally` clause, which ensures: - journal entries are still printed out to the log if there is any SystemExit - test config removal and original config restoration are ran regardless of what happens A `try...finally` clause is also used when applying the netplan config after restoring the original files to make sure journal entries are output to log regardless of the outcome. * netplan apply function does not return boolean anymore Fix #1674 * Catch FileNotFoundError when netplan test config absent * Use context managers to handle config backup/restoration * Make sure test config is deleted * Update unit tests
Description
Use context managers to handle original netplan config backup and restore, as well as the test config writing and removing.
Modify function
netplan_apply_configto raise SystemExit if thesubprocess call to netplan failed for any reason.
SystemExit
of what happens
Resolved issues
Fix #1674
https://warthogs.atlassian.net/browse/CHECKBOX-1718
Documentation
Tests
unit tests updated